package com.m1book.site;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.m1book.domain.relation.InviteTrace;
import com.m1book.domain.user.Miser;
import com.m1book.domain.user.MiserInfo;
import com.m1book.domain.user.MiserQQAccount;
import com.m1book.entity.RegisterInfo;
import com.m1book.repositories.user.MiserRepository;

@RequestMapping("/miser")
@Controller
@SessionAttributes("inviteTrace")
public class RegisterController {
	private static final Logger logger = LoggerFactory.getLogger(RegisterController.class);
	
	@Autowired
	private MiserRepository miserRepository;
	
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String register(Model model, HttpSession httpSession) {
		InviteTrace inviteTrace = (InviteTrace) httpSession.getAttribute("inviteTrace");
		if(inviteTrace != null) {
			// 如果是因为被邀请进入注册界面则可以注册。
			logger.info("register cause of " + inviteTrace.toString());
			
			RegisterInfo registerInfo = new RegisterInfo();
			registerInfo.setQq(inviteTrace.getQq());
			registerInfo.setRealname(inviteTrace.getInviteeName());
			model.addAttribute("registerinfo", registerInfo);
		} else {
			// 否则，提示必须从有邀请才能注册。
			return "/miser/noinviteregister";
		}
		return "/miser/register";
	}
	
	@RequestMapping(value = "/commiteregister", method = RequestMethod.POST)
	public String commitRegister(RegisterInfo registerInfo, Model model, HttpSession httpSession) {
		InviteTrace inviteTrace = (InviteTrace) httpSession.getAttribute("inviteTrace");
		if(inviteTrace != null) {
			// 如果是因为被邀请进入注册界面则可以注册。
			logger.info(registerInfo.toString());
			
			if (accountExist(registerInfo.getUsername())) {
				model.addAttribute("registerinfo", registerInfo);
				model.addAttribute("backreason", "用户名已经被使用，请换一个试试。");
				return "/miser/register";
			}
			
			Miser miser = new Miser();
			miser.setAccount(registerInfo.getUsername());
			miser.setPassword(registerInfo.getPassword());
			MiserQQAccount qqAccount = new MiserQQAccount();
			qqAccount.setQq(registerInfo.getQq());
			miser.setQqAccount(qqAccount);
			MiserInfo miserInfo = new MiserInfo();
			miserInfo.setRealname(registerInfo.getRealname());
			miserInfo.setNickname(registerInfo.getNickname());
			miser.setInfo(miserInfo);
			
			miserRepository.save(miser);
			
			// 设置此注册成功用户为登录状态。
			Utilities.setMiserLogin(miser);
		} else {
			// 否则，提示必须从有邀请才能注册。
			return "/miser/noinviteregister";
		}
		
		return "/miser/registerok";
	}
	
	private boolean accountExist(String account) {
		Miser exist = miserRepository.findByAccount(account);
		return exist != null;
	}
	
}
